package admin.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.Connect;
import model.Vocabulary;

public class VocabDAO {
	public static List<Vocabulary> getAllVocabularies(){
		ArrayList<Vocabulary> lisVocab = new ArrayList<Vocabulary>();
		String sql = "SELECT * FROM eonl_vocabulary ORDER BY id DESC";
		
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			
			while(result.next()){
				Vocabulary voca = new Vocabulary(result.getInt("id"),result.getString("name"), result.getString("title"), result.getString("content"));
				lisVocab.add(voca);
			}
			
			result.close();
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return lisVocab;
	}
	public static void deleteVocab(int id){
		String sql = "DELETE FROM `eonl_vocabulary` WHERE id="+id;
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
		}
	}
	public static void addVocab(Vocabulary vocabulary) {
		String sql = "INSERT INTO `eonl_vocabulary` (name,title,content) VALUES ('"+vocabulary.getName()+"','"+vocabulary.getTitle()+"','"+vocabulary.getContent()+"')";
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
		}
	}
	public static void updateVocab(Vocabulary vocab){
		String sql = "UPDATE `eonl_vocabulary` SET name='"+vocab.getName()+"',title='"+vocab.getTitle()+"',content='"+vocab.getContent()+"' WHERE id="+vocab.getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
